package com.software.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.software.dao.impl.MessageDetailDaoImpl;
import com.software.domain.MessageDetailBean;
import com.software.model.DrmAgentIntroduce;
import com.software.service.IntroduceService;
import com.software.utils.Constants;
import com.software.utils.DateUtils;
import com.software.utils.IdCreater;

@Controller("introduceController")
@RequestMapping(value = "/introduce/*")
public class IntroduceController extends BaseController{

	@Autowired
	private IntroduceService introduceService;
	
	@Autowired
	private MessageDetailDaoImpl mongo;

	/**
	 * 上传附件 测试用  业务不需要
	 * @param upload
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "upload")
	public String upload(@RequestParam MultipartFile upload, HttpServletResponse res, HttpServletRequest req){
		
		System.out.print(upload.getSize());
		
		return "";
	}
	
	/**
	 * 首页广告 保存图片
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "saveImg"  ,produces = "text/html;charset=UTF-8")
	public String saveImg(@RequestBody DrmAgentIntroduce imgData, HttpServletResponse res, HttpServletRequest req){
		
		imgData.setId(IdCreater.getId());
		// TODO
		imgData.setAgentId(super.getUserIdInSession(req));
		imgData.setUseYn(Constants.DEL_NO); 
		introduceService.saveIntroduce(imgData);
		return	"agent/addinfo";
	}
	
	/**
	 * 中心介绍 保存操作
	 * @param webdata
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "saveIntroduce"  ,produces = "text/html;charset=UTF-8")
	public ModelAndView saveIntroduce(@RequestBody DrmAgentIntroduce webdata, HttpServletResponse res, HttpServletRequest req){
		// 新增 修改表示  0:新增  1:修改
		int flag = 0;
		
		if(null == webdata.getId() || "".equals(webdata.getId().toString())) {
			webdata.setId(IdCreater.getId());
			flag = 0;
		} else {
			flag = 1;
		}
		
		
		// TODO
		webdata.setAgentId(super.getUserIdInSession(req ));
		webdata.setUseYn(Constants.DEL_NO); 
		
		
		MessageDetailBean bean = new MessageDetailBean();
		bean.setProductId((webdata.getId().toString()));
		bean.setCreateTime(DateUtils.getCurrentDateTime());
		bean.setDetailContent(webdata.getIntroduceTxt());
		bean.setSellerId(001);
		
		// 正文保存mongoDB
		MessageDetailBean newMongo = mongo.save(bean);
		
		webdata.setIntroduceTxt(newMongo.getId());
		
		if(0 == flag) {
			webdata.setId(IdCreater.getId());
			introduceService.saveIntroduce(webdata);
		} else if(1 == flag) {
			introduceService.updateIntroduce(webdata);
		}
		
		ModelAndView map = new ModelAndView();
		
		DrmAgentIntroduce webData = introduceService.queryIntroduce(webdata);
		webData.setIntroduceTxt(Constants.TXT_URL + webData.getIntroduceTxt());
		map.addObject("data",  webData);
		map.setViewName("agent/cent");
		
		return	map;
	}
	
	
}
